package frame.usermeau.service;

import frame.model.SnowflakeIdWorker;
import frame.model.Utils;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;

public class IdCardFrame extends JFrame {

    private String userId;

    public IdCardFrame(String userId) {
        this.setTitle("办理银行卡");
        //操作
        this.userId = userId;
        this.setContentPane(showNav());
        //设置窗口
        this.setBounds(580,280,400,300);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    /**
     * 显示UI，不用改变
     * @return
     */
    public JPanel showNav() {
        JPanel jPanel = new JPanel(null);
        int x = 30,xText = 130, y = 20;
        int width = 100, height = 20;

        JLabel jl;
        //办理银行卡类型
        jl = new JLabel("银行卡类型");
        jl.setLocation(x,y);
        jl.setSize(width,height);
        jPanel.add(jl);
        JComboBox type = new JComboBox();
        type.addItem("储蓄卡");
        type.addItem("信用卡");
        type.setLocation(xText,y);
        type.setSize(200,20);
        jPanel.add(type);
        y += 40;

        //密码
        jl = new JLabel("密码");
        jl.setLocation(x,y);
        jl.setSize(width,height);
        jPanel.add(jl);
        JPasswordField jTextAreaPasswd = new JPasswordField(20);
        jTextAreaPasswd.setLocation(xText,y);
        jTextAreaPasswd.setSize(200,20);
        jPanel.add(jTextAreaPasswd);
        y += 40;

        //确认密码
        jl = new JLabel("确认密码");
        jl.setLocation(x,y);
        jl.setSize(width,height);
        jPanel.add(jl);
        JPasswordField jTextAreaAgainPasswd = new JPasswordField(20);
        jTextAreaAgainPasswd.setLocation(xText,y);
        jTextAreaAgainPasswd.setSize(200,20);
        jPanel.add(jTextAreaAgainPasswd);
        y += 40;

        //添加按钮与事件
        JButton jb = new JButton("确认");
        jb.setLocation(xText+30,y);
        jb.setSize(60,40);
        jb.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String passwd = jTextAreaPasswd.getText();
                String aPasswd = jTextAreaAgainPasswd.getText();

                if(passwd.length() == 0 || aPasswd.length() == 0) {
                    JOptionPane.showMessageDialog(null, "请输入密码！");
                }else if(!passwd.equals(aPasswd)) {
                    JOptionPane.showMessageDialog(null, "密码验证错误！");
                } else {
                    handleService(type.getSelectedItem().toString(), passwd);
                }
            }
        });
        jPanel.add(jb);
        return jPanel;
    }


    /**
     * 办理业务
     * @param type      银行卡类型：信用卡，储蓄卡
     * @param passwd    密码
     */
    private void handleService(String type, String passwd) {
        init();
        //业务逻辑
        if ("储蓄卡".equals(type)) {
            try {
                PreparedStatement preparedStatement = conn.prepareStatement("insert into t_card(c_id, c_type, c_password, c_issue_date, c_user_id)" +
                        "    values (?,?,?,now(),?);");
                preparedStatement.setString(1,idWorker.nextId() + "77");
                preparedStatement.setInt(2,0);
                preparedStatement.setString(3, Utils.passwordMD5(passwd));
                preparedStatement.setString(4,userId);

                preparedStatement.execute();

                //弹窗返回结果
                String message = "银行卡添加成功";
                JOptionPane.showMessageDialog(null, message);
                this.dispose();

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else if ("信用卡".equals(type)) {
            try {
                PreparedStatement preparedStatement = conn.prepareStatement("insert into t_card(c_id, c_type, c_password, c_issue_date, c_user_id)" +
                        "    values (?,?,?,now(),?);");
                preparedStatement.setString(1,idWorker.nextId() + "77");
                preparedStatement.setInt(2,1);
                preparedStatement.setString(3, Utils.passwordMD5(passwd));
                preparedStatement.setString(4,userId);

                preparedStatement.execute();

                //弹窗返回结果
                String message = "银行卡添加成功";
                JOptionPane.showMessageDialog(null, message);
                this.dispose();

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    Connection conn;
    SnowflakeIdWorker idWorker;
    public void init() {
        try {
            Class.forName("org.opengauss.Driver");
            conn = DriverManager.getConnection("jdbc:opengauss://121.36.41.207:26000/finance", "geqiyu", "Tlyz0729");
            idWorker = new SnowflakeIdWorker(0, 0);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        try {
            // 设置demo为搜索路径
            PreparedStatement firstStep = conn.prepareStatement("SET SEARCH_PATH TO finance,public");
            firstStep.execute();
            firstStep.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
